<template>
    <div class="upload">
        <div class="title">{{$route.query.type == 0 ? '新建留声':'更新留声'}}</div>
        <div class="content">
             <form>
                <label>标题</label><el-input  class="username margin-bottom-20" v-model="title" placeholder='标题'></el-input>
                <label>描述</label><el-input  class="password margin-bottom-20" v-model="describeStr" placeholder='描述'></el-input>
                <!-- audio上传-->
                <el-upload
                ref="audio"
                class="upload-audio"
                action="http://127.0.0.1:3000/api/uploadfile/audios"
                :on-success="uploadAudioSuccess"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                multiple
                :limit="1"
                :on-exceed="handleExceed"
                :file-list="audioList"
                accept=".mp3,.m4a"
                :auto-upload="false"
                >
                <el-button size="small" type="primary">上传音频</el-button>
                </el-upload>
                <!-- video上传 -->
                <el-upload
                ref="video"
                :on-success="uploadVideoSuccess"
                class="upload-audio"
                action="http://127.0.0.1:3000/api/uploadfile/videos"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                multiple
                :limit="1"
                :on-exceed="handleExceed"
                :file-list="videoList"
                accept=".mp4"
                :auto-upload="false"
                >
                <el-button style="margin-top:30px" size="small" type="primary">上传视频</el-button>
                </el-upload>
                <el-button class="submit margin-bottom-20" type="primary" @click="submitUpload()"  size="default">提交</el-button>
            </form>
        </div>
    </div>
</template>
<script>
import axios from 'axios'
import { ElMessage } from 'element-plus'
export default {
    data() {
        return {
            title:'',
            describeStr:'',
            audioList:[],
            videoList:[],
            audioUrl:'',
            videoUrl:'',
            uploadProcess:2 //上传文件进程数 为0时表示上传完毕,此时可以获取到音频和视频的链接
        }
    },
    methods:{
        //把数据保存到数据库
        saveData(){
            // console.log(this.videoUrl)
            // console.log(this.audioUrl)
            let data = {
                username:this.$route.query.username,
                title:this.title,
                describeStr:this.describeStr,
                audioUrl:this.audioUrl,
                videoUrl:this.videoUrl,
                id:this.$route.query.id
            }

            //新增
            if(this.$route.query.type == 0){
                axios.post('http://127.0.0.1:3000/api/insert',data).then(res=>{
                    console.log(res.data)
                    if(res.data.code == 0){
                        console.log('success')
                        ElMessage.success({
                            message: '新建成功',
                            type: 'success'
                        })
                        this.$router.go(-1)
                        return;
                    }
                })
            }
            //更新
            if(this.$route.query.type == 1){
                axios.post('http://127.0.0.1:3000/api/update',data).then(res=>{
                    console.log(res.data)
                    if(res.data.code == 0){
                        console.log('success')
                        ElMessage.success({
                            message: '更新成功',
                            type: 'success'
                        })
                        this.$router.go(-1)
                        return;
                    }
                })
            }
            
        },
        //上传音频成功
        uploadAudioSuccess(response, file, fileList){
            console.log(response)
            this.audioUrl = response.data.url
            this.uploadProcess--
            if(this.uploadProcess == 0){
                this.saveData()
            }
        },
        // 上传视频成功
        uploadVideoSuccess(response, file, fileList){
            console.log(response)
            this.videoUrl = response.data.url
            this.uploadProcess--
            if(this.uploadProcess == 0){
                this.saveData()
            }
        },
        //上传所有文件
        submitUpload() {
            this.$refs.audio.submit();
            this.$refs.video.submit();
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePreview(file) {
            console.log(file);
        },
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
        },
        beforeRemove(file, fileList) {
            return this.$confirm(`确定移除 ${ file.name }？`);
        }
    }
}
</script>
<style lang="less" scoped>
.submit{
    width: 50%;
    margin-left: 25%;
    margin-top: 20px;
}
form label{
    line-height: 20px;
    height: 20px;
    display: block;
    margin-bottom: 5px;
    color: #66B1FF;
    font-size: 16px;
}
.margin-bottom-20{
    margin-bottom: 20px;
}
.content{
    padding-top: 50px;
    padding-inline: 30%;
    width: 100%;
    height: calc(100% - 30px);
    // background-color: rosybrown;
}
.title{
    font-size: 18px;
    width: 100%;
    height: 30px;
    // background-color: red;
    // text-align: center;
    border-bottom: solid 1px rgba(0, 0, 0,0.1);
}
.upload{
    width: 100%;
    height: 100%;
    padding: 20px;
    border: solid 1px rgba(0, 0, 0,0.1);
}
</style>